<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
			@close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
				<el-form-item label="所属语言" prop="lang_id">
					<el-select class="flex-1" v-model="formData.lang_id" placeholder="请选择所属语言" clearable>
						<el-option label="通用" :value="0" />
						<el-option v-for="item in optionsData.languages" :key="item.id" :label="item.name"
							:value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="类型" prop="type">
					<el-select class="flex-1" :disabled="formData.id!=''" v-model="formData.type" clearable
						placeholder="请选择类型">
						<el-option v-for="(item, index) in dictData.recharge_type" :key="index" :label="item.name"
							:value="parseInt(item.value)" />
					</el-select>
				</el-form-item>
				<el-form-item label="LOGO" prop="logo">
					<material-picker v-model="formData.logo" />
				</el-form-item>
				<el-form-item label="名称" prop="name">
					<el-input v-model="formData.name" clearable placeholder="请输入名称" />
				</el-form-item>
				<el-form-item v-if="formData.type == 5" label="主币种编号" prop="main_coin_type">
					<el-input v-model="formData.main_coin_type" clearable placeholder="请输入主币种编号" />
				</el-form-item>
				<el-form-item v-if="formData.type == 5" label="子币种编号" prop="coin_type">
					<el-input v-model="formData.coin_type" clearable placeholder="请输入子币种编号" />
				</el-form-item>
				<el-form-item v-if="formData.type == 5 ||formData.type == 6" label="币种协议" prop="protocol">
					<el-input v-model="formData.protocol" clearable placeholder="请输入币种协议" />
				</el-form-item>
				<el-form-item label="账号" prop="account" v-if="formData.type==1 ||formData.type==2 ||formData.type==3">
					<el-input v-model="formData.account" clearable placeholder="请输入账号" />
				</el-form-item>
				<el-form-item v-if="formData.type==1 ||formData.type==2" label="二维码" prop="img">
					<material-picker v-model="formData.img" />
				</el-form-item>
				<el-form-item v-if="formData.type==3" label="银行名称" prop="bank_name">
					<el-input v-model="formData.bank_name" clearable placeholder="请输入银行名称" />
				</el-form-item>
				<el-form-item v-if="formData.type==3" label="持卡人姓名" prop="bank_username">
					<el-input v-model="formData.bank_username" clearable placeholder="请输入持卡人姓名" />
				</el-form-item>
				<el-form-item v-if="formData.type==7" label="地址" prop="bank_username">
					<el-input v-model="formData.address" :rows="3" type="textarea" clearable placeholder="请输入地址" />
					<div class="form-tips">多个请用换行隔开</div>
				</el-form-item>
				<el-form-item v-if="formData.type == 7" label="币种协议" prop="protocol">
					<el-input v-model="formData.protocol" clearable placeholder="请输入币种协议" />
				</el-form-item>
				<el-form-item v-if="formData.type==7" label="会员等级" prop="member_id">
					<el-select class="flex-1" v-model="formData.member_id" placeholder="请选择会员等级" clearable>
						<el-option v-for="item in optionsData.members" :key="item.id" :label="item.name"
							:value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item v-if="formData.type == 7" label="可获取次数" prop="avail_num">
					<el-input v-model="formData.avail_num" type="number" placeholder="请输入可获取次数" />
				</el-form-item>
				<el-form-item label="货币符号" prop="symbol">
					<el-input v-model="formData.symbol" clearable placeholder="请输入货币符号" />
				</el-form-item>
				<el-form-item label="汇率" prop="rate">
					<el-input v-model="formData.rate" type="number" clearable placeholder="请输入汇率" />
				</el-form-item>
				<el-form-item v-if="formData.type==1 ||formData.type==5 ||formData.type==7" label="汇率标识" prop="symbol_rate">
					<el-input v-model="formData.symbol_rate" clearable placeholder="请输入汇率标识" />
					<div class="form-tips">用于自动更新汇率，汇率为1或无需更新可不填</div>
				</el-form-item>
				<el-form-item label="精度" prop="rate">
					<el-input v-model="formData.precision" type="number" clearable placeholder="请输入精度" />
				</el-form-item>
				<el-form-item label="需上传凭证" prop="is_voucher"
					v-if="formData.type==1 ||formData.type==2 ||formData.type==3 ||formData.type==7">
					<el-select class="flex-1" v-model="formData.is_voucher" clearable placeholder="请选择是否需要上传凭证">
						<el-option label="需要" :value="1" />
						<el-option label="不需要" :value="0" />
					</el-select>
				</el-form-item>
				<el-form-item label="是否显示" prop="is_show">
					<el-select class="flex-1" v-model="formData.is_show" clearable placeholder="请选择是否显示">
						<el-option v-for="(item, index) in dictData.show_status" :key="index" :label="item.name"
							:value="parseInt(item.value)" />
					</el-select>
				</el-form-item>
				<el-form-item label="排序" prop="sort">
					<el-input v-model="formData.sort" type="number" clearable placeholder="请输入排序" />
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>

<script lang="ts" setup name="rechargeMethodEdit">
	import type { FormInstance } from 'element-plus'
	import Popup from '@/components/popup/index.vue'
	import { useDictOptions } from '@/hooks/useDictOptions'
	import { apiRechargeMethodAdd, apiRechargeMethodEdit, apiRechargeMethodDetail } from '@/api/setting/recharge_method'
	import { apiLanguageAll } from '@/api/setting/language'
	import { apiUserMemberAll } from '@/api/user_member'
	import { timeFormat } from '@/utils/util'
	import type { PropType } from 'vue'
	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')


	// 弹窗标题
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑充值方式' : '新增充值方式'
	})

	// 表单数据
	const formData = reactive({
		id: '',
		lang_id: 0,
		type: 1,
		logo: '',
		name: '',
		account: '',
		img: '',
		bank_name: '',
		bank_username: '',
		rate: 1,
		symbol_rate:'',
		is_show: 1,
		is_voucher: 0,
		sort: 0,
		symbol: 'USDT',
		precision: 2,
		main_coin_type: '',
		coin_type: '',
		protocol: '',
		address: '',
		member_id: '',
		avail_num: 10
	})


	// 表单验证
	const formRules = reactive<any>({
		lang_id: [{
			required: true,
			message: '请输入所属语言',
			trigger: ['blur']
		}],
		type: [{
			required: true,
			message: '请选择类型',
			trigger: ['blur']
		}],
		name: [{
			required: true,
			message: '请输入名称',
			trigger: ['blur']
		}],
		rate: [{
			required: true,
			message: '请输入汇率',
			trigger: ['blur']
		}],
		precision: [{
			required: true,
			message: '请选择精度',
			trigger: ['blur']
		}]
	})


	// 获取详情
	const setFormData = async (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}
	}

	const getDetail = async (row : Record<string, any>) => {
		const data = await apiRechargeMethodDetail({
			id: row.id
		})
		setFormData(data)
	}

	const { optionsData } = useDictOptions<{
		languages : any[]
		members : any[]
	}>({
		languages: {
			api: apiLanguageAll
		},
		members: {
			api: apiUserMemberAll
		}
	})


	// 提交按钮
	const handleSubmit = async () => {
		await formRef.value?.validate()
		const data = { ...formData, }
		mode.value == 'edit'
			? await apiRechargeMethodEdit(data)
			: await apiRechargeMethodAdd(data)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}



	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>